#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
工伤鉴定书分析API启动脚本
"""

import os
import sys
import subprocess
from pathlib import Path

def check_dependencies():
    """检查依赖包是否已安装"""
    required_packages = [
        'fastapi',
        'uvicorn',
        'easyocr',
        'openai',
        'PyPDF2',
        'python-docx',
        'numpy',
        'cv2'
    ]
    
    missing_packages = []
    
    for package in required_packages:
        try:
            if package == 'cv2':
                import cv2
            else:
                __import__(package)
        except ImportError:
            missing_packages.append(package)
    
    if missing_packages:
        print("❌ 缺少以下依赖包:")
        for pkg in missing_packages:
            print(f"  - {pkg}")
        print("\n请运行以下命令安装依赖:")
        print("pip install -r requirements.txt")
        return False
    
    print("✅ 所有依赖包已安装")
    return True

def start_api_server():
    """启动API服务器"""
    try:
        print("正在启动工伤鉴定书分析API服务...")
        print("服务地址: http://localhost:8000")
        print("API文档: http://localhost:8000/docs")
        print("按 Ctrl+C 停止服务\n")
        
        # 启动uvicorn服务器
        subprocess.run([
            sys.executable, "-m", "uvicorn", 
            "injury_assessment_api:app",
            "--host", "0.0.0.0",
            "--port", "8000",
            "--reload"
        ])
        
    except KeyboardInterrupt:
        print("\n服务已停止")
    except Exception as e:
        print(f"❌ 启动服务时出错: {str(e)}")

def main():
    """主函数"""
    print("工伤鉴定书分析API启动器")
    print("=" * 40)
    
    # 检查当前目录
    current_dir = Path(".")
    api_file = current_dir / "injury_assessment_api.py"
    
    if not api_file.exists():
        print(f"❌ 未找到API文件: {api_file}")
        print("请确保在正确的目录下运行此脚本")
        return
    
    # 检查依赖
    print("检查依赖包...")
    if not check_dependencies():
        return
    
    # 启动服务
    start_api_server()

if __name__ == "__main__":
    main()